﻿//<my-matriculator - a simple tool for registering racers in a USAC cyclocross race>
//Copyright (C) <2011>  <Steven Rivers>

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Registration.Model;
using Registration.Model.Entities;
using Registration.Printing;

namespace Registration
{
    /// <summary>
    /// Interaction logic for StartList.xaml
    /// </summary>
    public partial class StartList
    {
        private bool Group { get; set; }
        public ICollection<Field> Fields { get; set; }

        public StartList()
        {
            Group = false;
            InitializeComponent();
            Fields = FieldsDb.GetAllFields();
            cmbField.ItemsSource = Fields;
            Refresh(-1);
            Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("./background.jpg", UriKind.Relative)) };
            Opacity = .98;

        }

        private void OnComboChanged(object sender, EventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {
            var f = ((Field)cmbField.SelectedItem);
            if (f != null)
                Refresh(f.FieldId);
        }

        private void Refresh(int fieldid)
        {
            ICollection<Entry> entries = EntryDb.GetAllEntriesForField(fieldid, Group);
            listResults.ItemsSource = entries;
        }

        private void OnPrintButton(object sender, RoutedEventArgs e)
        {
            var f = (Field)cmbField.SelectedItem;
            if (f == null) return;
            var entries = EntryDb.GetAllEntriesForField(f.FieldId, Group);
            var table = CollectionHelper.ConvertTo(entries);

            var printDialog = new PrintDialog();
            if (printDialog.ShowDialog() != true) return;
            var paginator = new StartListDataSetPaginator(entries, table,
                                                          new Typeface(GlobalData.FontName), GlobalData.FontSize, 96 * 0.75,
                                                          new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight));

            printDialog.PrintDocument(paginator, "Start List For - xyz");
        }

        private void ClickGroup(object sender, RoutedEventArgs e)
        {
            if (checkGroup.IsChecked == null) return;
            Group = checkGroup.IsChecked.Value;
            Refresh();
        }

    }


}
